/*
 * Created on 20010-2-10
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sitech.portal.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.ibnms.util.LabelValueBean;
import com.sitech.portal.system.domain.TblSYS_ROLE;
import com.sitech.portal.system.form.RolePortalForm;
import com.sitech.portal.system.service.ISysRolePortalService;
import com.sitech.portal.system.service.ISysRoleService;


/**
 * @author niezl
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class RolePortalAction extends JGridBaseAction {
	
	private RolePortalForm theForm;
	private ISysRolePortalService sysRolePortalService;
	private ISysRoleService sysRoleService;
	private String flag;

	public RolePortalForm getTheForm() {
		return theForm;
	}

	public void setTheForm(RolePortalForm theForm) {
		this.theForm = theForm;
	}

	public ISysRolePortalService getSysRolePortalService() {
		return sysRolePortalService;
	}

	public void setSysRolePortalService(ISysRolePortalService sysRolePortalService) {
		this.sysRolePortalService = sysRolePortalService;
	}

	public ISysRoleService getSysRoleService() {
		return sysRoleService;
	}

	public void setSysRoleService(ISysRoleService sysRoleService) {
		this.sysRoleService = sysRoleService;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String loadListRolePortal() {
		IPage result = sysRolePortalService.findListRolePortal(this.getPageForm());
		this.setResult(result);
		return SUCCESS;
	}

	public String editRolePortal() {
		String[] PORTAL_ORDERS = theForm.getPORTAL_ORDERS();
		// String[] ROLE_IDSS = theForm.getROLE_IDSS();
		if (theForm.getISMP_JSP_COMMAND() != null
				&& theForm.getISMP_JSP_COMMAND().equals("add")) {
			List canSelectElementList = sysRoleService.findForListToAll();
			List rList = getRoleOptions(canSelectElementList);

			List canSelectPanel = sysRolePortalService.findAllPanel();
			List sList = getPanelOptions(canSelectPanel);
			theForm.setCanRoleList(rList);
			theForm.setCanPanelList(sList);
			theForm.setROLE_IDS(null);
			return SUCCESS;

		} else if (theForm.getISMP_JSP_COMMAND() != null
				&& theForm.getISMP_JSP_COMMAND().equals("modify")) {

			HashMap rolePortalMap = sysRolePortalService
					.findObjForTB_SYS_ROLE_PORTALByPK(PORTAL_ORDERS[0]);
			String ROLE_IDS = (String) rolePortalMap.get("ROLE_IDS");
			// 1.还能选择的角色(去掉现在已经拥有的角色)
			// 2.已经选择了的角色
			String selectedRole = "";
			String selectedPanel = "";
			String PANEL_COLLECT = "";
			String[] ROLE_LIST = ROLE_IDS.split("#");
			for (int i = 0; i < ROLE_LIST.length; i++) {
				selectedRole += ROLE_LIST[i] + ",";
			}
			selectedRole = selectedRole.substring(0, selectedRole.length() - 1);

			List canSelectElementList = sysRolePortalService.findCanRoles(selectedRole);
			List selectedElementList = sysRolePortalService.findSelectRoles(selectedRole);
			List rList = getRoleOptions(canSelectElementList);
			List dList = getRoleOptions(selectedElementList);
			theForm.setCanRoleList(rList);
			theForm.setRoleList(dList);
			// 3.还能选择的Panel(去掉现在已经拥有的Panel)
			// 4.已经选择了的Panel

			PANEL_COLLECT = rolePortalMap.get("PANEL_COLLECT").toString();
			String[] sPanel = PANEL_COLLECT.split(",");
			for (int j = 0; j < sPanel.length; j++) {
				selectedPanel += "'" + sPanel[j] + "',";
			}
			selectedPanel = selectedPanel.substring(0,
					selectedPanel.length() - 1);
			List canSelectPanel = sysRolePortalService.findCanPanel(selectedPanel);
			List sList = getPanelOptions(canSelectPanel);

			List selectedPane = sysRolePortalService.findSelectPanel(selectedPanel);
			List eList = getPanelOptions(selectedPane);

			theForm.setCanPanelList(sList);
			theForm.setPanelList(eList);
			theForm.setROLE_NAMES(rolePortalMap.get("ROLE_NAMES").toString());
			theForm.setENABLE(rolePortalMap.get("ENABLE").toString());
			theForm.setPORTAL_ORDER(PORTAL_ORDERS[0]);
		}

		return SUCCESS;
	}

	public String saveRolePortal() {
		flag = "true";
		
		try{
			HashMap map = new HashMap();
			String PANEL_COLLECT = "";
			String selectPanels = theForm.getSelectPanels();
			String selectRoles = theForm.getSelectRoles();
	
			String[] selectPanel = selectPanels.split("#");
			for (int j = 0; j < selectPanel.length; j++) {
				PANEL_COLLECT += selectPanel[j] + ",";
			}
			String[] selectRole = selectRoles.split("#");
			String ROLE_IDS = "";
			String ROLE_NAMES = "";
			for (int i = 0; i < selectRole.length; i++) {
				// 取得对应角色中文名
				String ROLE_NAME = sysRoleService.findForTB_SYS_ROLEByRoleId(
						selectRole[i]).getROLE_NAME();
				if (i < selectRole.length - 1) {
					ROLE_IDS += selectRole[i] + "#";
					ROLE_NAMES += ROLE_NAME + "#";
				} else {
					ROLE_IDS += selectRole[i];
					ROLE_NAMES += ROLE_NAME;
				}
			}
			map.put("ROLE_IDS", ROLE_IDS);
			map.put("ROLE_NAMES", ROLE_NAMES);
			map.put("ENABLE", theForm.getENABLE());
			map.put("PANEL_COLLECT", PANEL_COLLECT);
			if (theForm.getISMP_JSP_COMMAND() != null
					&& theForm.getISMP_JSP_COMMAND().equals("add")) {
				sysRolePortalService.add(map);
			} else if (theForm.getISMP_JSP_COMMAND() != null
					&& theForm.getISMP_JSP_COMMAND().equals("modify")) {
				String portal_order = theForm.getPORTAL_ORDER();
				map.put("PORTAL_ORDER", portal_order);
				sysRolePortalService.update(map);
			}
		}catch(Exception e){
			flag = e.getMessage();
		}

		return SUCCESS;
	}

	public String delRolePortal() {
		flag = "true";
		
		try{
			String[] PORTAL_ORDERS = theForm.getPORTAL_ORDERS();
			try {
				for (int i = 0; i < PORTAL_ORDERS.length; i++) {
					sysRolePortalService
							.deleteForTB_SYS_ROLE_PORTALByPK(PORTAL_ORDERS[i]);
				}
			} catch (Exception e) {
				flag = e.getMessage();
			}
		}catch(Exception e){
			flag = e.getMessage();
		}

		return SUCCESS;
	}

	private List getRoleOptions(List roles) {
		List list = new ArrayList();
		for (int i = 0; i < roles.size(); i++) {
			TblSYS_ROLE item = (TblSYS_ROLE) roles.get(i);
			list
					.add(new LabelValueBean(item.getROLE_NAME(), item
							.getROLE_ID()));
		}
		return list;
	}

	private List getPanelOptions(List roles) {
		List list = new ArrayList();
		HashMap map = new HashMap();
		for (int i = 0; i < roles.size(); i++) {
			map = (HashMap) roles.get(i);
			list.add(new LabelValueBean(map.get("PANEL_NAME").toString(), map
					.get("PANEL_ID").toString()));
		}
		return list;
	}
}